//
//  XHTabBar.swift
//  XHWeiboSwift
//
//  Created by mac on 17/1/16.
//  Copyright © 2017年 mac.gu. All rights reserved.
//

import UIKit

class XHTabBar: UITabBar {

   
    var closeure: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
        addSubview(addButton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = UIScreen.main.bounds.width * 0.2
        
        var index :CGFloat = 0
        
        
        for view in subviews {
            
            if view .isKind(of: NSClassFromString("UITabBarButton")!) {
                
                //设置约束
                view.frame.size.width = w
                view.frame.origin.x = w * index
                index += 1
                if index == 2 {
                    index += 1
                }
                
            }
        }
        
        //设置中间按钮约束
        addButton.center.x = frame.width * 0.5
        addButton.center.y = frame.height * 0.5
        
    }
    
    private lazy var addButton: UIButton = {
        
        let addBtn = UIButton()
        
        //默认
        addBtn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: UIControlState.normal)
        //选中
        addBtn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        //背景图片
        addBtn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: UIControlState.normal)
        
        //高亮
        addBtn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        
        addBtn.sizeToFit()
    
        
        //添加点击事件
        addBtn.addTarget(self, action:#selector(addBtnClick) , for: UIControlEvents.touchUpInside)
        
        return addBtn
    }()
    
    
    
    @objc private func addBtnClick(){
        
        closeure?()
        
    }
    
    
    
    
    
    
    
    
    
    
}
